/*!
\copyright  Copyright (c) 2019 - 2024 Qualcomm Technologies International, Ltd.
            All Rights Reserved.
            Qualcomm Technologies International, Ltd. Confidential and Proprietary.
\file
\brief
*/

#include "a2dp_profile_audio.h"
#include "a2dp_profile_audio_params.h"
#include "av.h"
#include <logging.h>

static bool a2dpProfile_IsAudioRouted(audio_source_t source);
static bool a2dpProfile_IsAudioAvailable(audio_source_t source);
static source_status_t a2dpProfile_SetState(audio_source_t source, source_state_t state);
static source_state_t a2dpProfile_GetState(audio_source_t source);
static device_t a2dpProfile_GetAudioSourceDevice(audio_source_t source);

static const audio_source_audio_interface_t handset_if =
{
    .GetConnectParameters = A2dpProfile_GetHandsetSourceAudioConnectParameters,
    .ReleaseConnectParameters = A2dpProfile_FreeAudioConnectParameters,
    .GetDisconnectParameters = A2dpProfile_GetHandsetSourceAudioDisconnectParameters,
    .ReleaseDisconnectParameters = A2dpProfile_FreeAudioDisconnectParameters,
    .IsAudioRouted = a2dpProfile_IsAudioRouted,
    .IsAudioAvailable = a2dpProfile_IsAudioAvailable,
    .SetState = a2dpProfile_SetState,
    .GetState = a2dpProfile_GetState,
    .Device = a2dpProfile_GetAudioSourceDevice,
};

static bool a2dpProfile_IsAudioRouted(audio_source_t source)
{
    bool is_audio_routed = FALSE;

    avInstanceTaskData *av_instance = Av_GetInstanceForHandsetSource(source);

    if(av_instance && (av_instance->a2dp.source_state == source_state_connected))
    {
        is_audio_routed = TRUE;
    }

    DEBUG_LOG("a2dpProfile_IsAudioRouted source enum:audio_source_t:%d, is_audio_routed=%d", source, is_audio_routed);

    return is_audio_routed;
}

const audio_source_audio_interface_t * A2dpProfile_GetHandsetSourceAudioInterface(void)
{
    return &handset_if;
}

static source_status_t a2dpProfile_SetState(audio_source_t source, source_state_t state)
{
    avInstanceTaskData *av_instance = Av_GetInstanceForHandsetSource(source);

    DEBUG_LOG_FN_ENTRY("a2dpProfile_SetState enum:audio_source_t:%d enum:source_state_t:%d", source, state);

    if(av_instance)
    {
        return A2dpProfile_SetSourceState(av_instance, state);
    }
    return source_status_ready;
}

static source_state_t a2dpProfile_GetState(audio_source_t source)
{
    source_state_t state = source_state_invalid;
    avInstanceTaskData *av_instance = Av_GetInstanceForHandsetSource(source);

    DEBUG_LOG_FN_ENTRY("a2dpProfile_GetState enum:audio_source_t:%d", source);

    if(av_instance)
    {
        state =  A2dpProfile_GetSourceState(av_instance);
    }
    return state;
}

static device_t a2dpProfile_GetAudioSourceDevice(audio_source_t source)
{
    const avInstanceTaskData *av = Av_GetInstanceForHandsetSource(source);

    if (av)
    {
        return BtDevice_GetDeviceForBdAddr(&(av->bd_addr));
    }

    return NULL;
}

static bool a2dpProfile_IsAudioAvailable(audio_source_t source)
{
    bool is_audio_available = FALSE;

    avInstanceTaskData *av_instance = Av_GetInstanceForHandsetSource(source);

    if(av_instance && Av_InstanceIsA2dpSinkStarted(av_instance) && !Av_IsInstancePaused(av_instance))
    {
        is_audio_available = TRUE;
    }

    DEBUG_LOG("a2dpProfile_IsAudioAvailable source enum:audio_source_t:%d, is_audio_available=%d", source, is_audio_available);

    return is_audio_available;
}
